using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Sauce.Internal;
using Voodoo.Sauce.Internal.DebugScreen;

namespace Voodoo.Sauce.Debugger
{
	public class LogConsoleDebugScreen : Screen
	{
		[Serializable]
		internal class LogTypeFields
		{
			public Toggle toggle;

			public Color color;

			public Text titleText;

			[NonSerialized]
			public int count;
		}

		[SerializeField]
		private Transform issueListItemParent;

		[SerializeField]
		private DebugLogMessageListItem issueListItemPrefab;

		[SerializeField]
		private DebugLogMessagePopup logMessagePopup;

		[SerializeField]
		private Button clearAllButton;

		[SerializeField]
		private Button optionButton;

		[SerializeField]
		private LogsOptionDebugScreen optionScreen;

		[SerializeField]
		private ScrollRect issuesScrollRect;

		[SerializeField]
		private Button scrollDownButton;

		[SerializeField]
		private InputField searchInputField;

		[SerializeField]
		private Text searchCountText;

		[SerializeField]
		private LogTypeFields exceptionFields;

		[SerializeField]
		private LogTypeFields errorFields;

		[SerializeField]
		private LogTypeFields warningFields;

		private static string _searchString;

		private Dictionary<LogType, LogTypeFields> _logTypeDict;

		private readonly List<DebugLogMessageListItem> _issueMessageList = new List<DebugLogMessageListItem>();

		public static Action Closed;

		private void Awake()
		{
			optionButton.onClick.AddListener(delegate
			{
				Debugger.Show(optionScreen);
			});
			_logTypeDict = new Dictionary<LogType, LogTypeFields>
			{
				{
					LogType.Exception,
					exceptionFields
				},
				{
					LogType.Error,
					errorFields
				},
				{
					LogType.Warning,
					warningFields
				}
			};
			logMessagePopup.Hide();
			foreach (KeyValuePair<LogType, LogTypeFields> item in _logTypeDict)
			{
				LogType logType = item.Key;
				LogTypeFields value = item.Value;
				value.toggle.isOn = DebugLogTypes.IsEnabled(logType);
				value.toggle.onValueChanged.AddListener(delegate (bool isToggled)
				{
					ToggleLogType(isToggled, logType);
				});
				value.toggle.gameObject.transform.GetChild(0).gameObject.GetComponent<Image>().color = value.color;
			}
			searchInputField.onEndEdit.AddListener(OnSearchEndEdit);
			clearAllButton.onClick.AddListener(delegate
			{
				//LogsTracker.Clear();
				ClearExceptionsList();
			});
			scrollDownButton.onClick.AddListener(delegate
			{
				issuesScrollRect.normalizedPosition = new Vector2(0f, 0f);
			});
		}

		private void OnLogEnableChange(bool value)
		{
			VoodooLog.EnableDebugLogs(value);
		}

		private void OnDestroy()
		{
			searchInputField.onEndEdit.RemoveAllListeners();
			clearAllButton.onClick.RemoveAllListeners();
			scrollDownButton.onClick.RemoveAllListeners();
		}

		private int CountActiveMessageListItems()
		{
			return _issueMessageList.Count((DebugLogMessageListItem messageListItem) => messageListItem.gameObject.activeInHierarchy);
		}

		private void OnSearchEndEdit(string searchString)
		{
			_searchString = searchString.ToLower();
			UpdateMessageList();
			UpdateSearchCountText(CountActiveMessageListItems());
		}

		private void UpdateMessageList()
		{
			foreach (DebugLogMessageListItem issueMessage in _issueMessageList)
			{
				if (MessageMatchesSearchText(issueMessage.LogMessage))
				{
					bool active = DebugLogTypes.IsEnabled(issueMessage.LogMessage.logType);
					issueMessage.gameObject.SetActive(active);
				}
				else
				{
					issueMessage.gameObject.SetActive(value: false);
				}
			}
		}

		private void OnEnable()
		{
			//LogsTracker.OnLogReceived += LogsTrackerOnIssueMessageReceived;
			InitIssuesList();
		}

		private void OnDisable()
		{
			//LogsTracker.OnLogReceived -= LogsTrackerOnIssueMessageReceived;
			ClearExceptionsList();
			Closed?.Invoke();
		}

		private void InitIssuesList()
		{
			// IReadOnlyCollection<LogMessage> issues = LogsTracker.Issues;
			// if (issues == null)
			// {
			// 	return;
			// }
			int num = 0;
			// foreach (LogMessage item in issues)
			// {
			// 	DebugLogMessageListItem debugLogMessageListItem = AddLogMessage(item);
			// 	if (!(debugLogMessageListItem == null))
			// 	{
			// 		if (DebugLogTypes.IsEnabled(item.logType) && MessageMatchesSearchText(item))
			// 		{
			// 			num++;
			// 		}
			// 		else
			// 		{
			// 			debugLogMessageListItem.gameObject.SetActive(value: false);
			// 		}
			// 	}
			// }
			// UpdateSearchCountText(num);
			// foreach (KeyValuePair<LogType, LogTypeFields> item2 in _logTypeDict)
			// {
			// 	SetLogTypeCount(item2.Key, LogsTracker.CountFor(item2.Key));
			// }
		}

		private void ShowLogMessagePopup(LogMessage logMessage)
		{
			logMessagePopup.Show(logMessage);
		}

		private void SetLogTypeCount(LogType logType, int count = -1)
		{
			LogTypeFields logTypeFields = _logTypeDict[logType];
			logTypeFields.titleText.text = logType.ToString() + " (" + count + ")";
			logTypeFields.count = count;
		}

		private void IncrementLogTypeCount(LogType logType)
		{
			_logTypeDict[logType].count++;
			SetLogTypeCount(logType, _logTypeDict[logType].count++);
		}

		private void ToggleLogType(bool isActive, LogType logType)
		{
			foreach (DebugLogMessageListItem item in _issueMessageList.Where((DebugLogMessageListItem messageListItem) => messageListItem.LogMessage.logType == logType))
			{
				item.gameObject.SetActive(isActive && MessageMatchesSearchText(item.LogMessage));
			}
			DebugLogTypes.Update(logType, isActive);
			UpdateSearchCountText(CountActiveMessageListItems());
		}

		private void ClearExceptionsList()
		{
			foreach (Transform item in issueListItemParent)
			{
				UnityEngine.Object.Destroy(item.gameObject);
			}
			_issueMessageList.Clear();
			UpdateSearchCountText(0);
			foreach (KeyValuePair<LogType, LogTypeFields> item2 in _logTypeDict)
			{
				SetLogTypeCount(item2.Key, 0);
			}
		}

		private void LogsTrackerOnIssueMessageReceived(LogMessage issueMessage)
		{
			AddLogMessage(issueMessage);
		}

		private DebugLogMessageListItem AddLogMessage(LogMessage issueMessage)
		{
			if (!_logTypeDict.ContainsKey(issueMessage.logType))
			{
				return null;
			}
			DebugLogMessageListItem debugLogMessageListItem = UnityEngine.Object.Instantiate(issueListItemPrefab, issueListItemParent);
			debugLogMessageListItem.Initialize(_logTypeDict[issueMessage.logType].color, issueMessage, OnMessageListClicked);
			_issueMessageList.Add(debugLogMessageListItem);
			IncrementLogTypeCount(issueMessage.logType);
			debugLogMessageListItem.gameObject.SetActive(DebugLogTypes.IsEnabled(issueMessage.logType));
			if (!MessageMatchesSearchText(issueMessage))
			{
				debugLogMessageListItem.gameObject.SetActive(value: false);
			}
			return debugLogMessageListItem;
		}

		private void OnMessageListClicked(LogMessage logMessage)
		{
			ShowLogMessagePopup(logMessage);
		}

		public static bool MessageMatchesSearchText(LogMessage issueMessage)
		{
			if (!string.IsNullOrWhiteSpace(_searchString) && !issueMessage.message.ToLower().Contains(_searchString))
			{
				return issueMessage.stacktrace.ToLower().Contains(_searchString);
			}
			return true;
		}

		private void UpdateSearchCountText(int count)
		{
			searchCountText.text = $"[{count} results]";
		}

		public override BadgeCounter Counter()
		{
			return new IssuesCounter();
		}
	}
}
